package com.zcily.demo.controller;

import com.zcily.demo.entity.dto.Result;
import com.zcily.demo.entity.vo.StorageObject;
import com.zcily.demo.service.MinIOService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/minio")
@Api(tags = "minio文件存储")
@Slf4j
public class MinIOController {

    @Autowired
    private MinIOService minIOService;

    /**
     * 上传文件
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "上传文件")
    public Result<StorageObject> upload(@RequestPart("file") MultipartFile file) {
        // 上传文件
        StorageObject storageObject = minIOService.upload(file);
        return Result.ok(storageObject);
    }

    /**
     * 预览文件
     */
    @GetMapping("/preview")
    @ApiOperation(value = "预览文件")
    public String preview(String fileName) {
        return minIOService.getFileUrl(fileName);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    @ApiOperation(value = "下载文件")
    public void download(String fileName, HttpServletResponse response) {
        minIOService.download(response, fileName);
    }

    /**
     * 删除文件
     */
    @GetMapping("/delete")
    @ApiOperation(value = "删除文件")
    public String delete(String fileName) {
        minIOService.delete(fileName);
        return "删除成功";
    }
}
